import type { AddressItem, AddressParams } from "@/types/address"
import { http } from "@/utils/https"
import { useAddressStore } from '@/stores/modules/address';

/**
 *  添加收货地址
 * @param data 请求参数
 * @returns
 */
export const postMemberAddressAPI = (data: AddressParams) => {
  console.log(data)
  return http({
    method: 'POST',
    url: '/address',
    data,
  })
}
/**
 *  修改收货地址
 * @param id 地址的id
 * @param data 请求参数
 * @returns
 */
export const postEditAddressAPI = (id: string, data: AddressParams) => {
  return http<AddressItem>({
    method: 'POST',
    url: `/address/edit?id=${id}`,
    data,
  })
}
/**
 *
 * @param userId 用户id参数
 * @returns
 */
// 获取收货地址列表
export const getMemberAddressAPI = async (userId: string) => {
  const addressStore = useAddressStore()
  const res = await http<AddressItem[]>({
    method: 'GET',
    url: `/address/userId?userId=${userId}`
  })
  if (res.code === 2000) {
    addressStore.changeDefaultAddress(res.data.filter(v => { return v.isDefault === true })[0])
  }
  return res
}

/**
 *
 * @param id 地址id参数
 * @returns
 */
//获取收货地址详情
export const getAddressInfoAPI = (id: string) => {
  return http<AddressItem[]>({
    method: "GET",
    url: `/address/id?id=${id}`
  })
}


/**
 *  删除收货地址
 * @param id 地址的id
 * @returns
 */
export const deleteMemberAddressByIdAPI = (id: string) => {
  return http({
    method: 'DELETE',
    url: `/address?id=${id}`,
  })
}
